import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

// 通过三种方式构造http请求：1>输入地址栏get请求 2>postman构造http请求 3> ajax构造http请求
@WebServlet("/method")
public class MethodServlet extends HttpServlet {
//    因为这些doGet，doPost等方法是服务器自动调用的方法，所以不需要我们手动用service去规划通往哪个方法的路线
//    源码中的service就已经帮我们规划好哪个请求走哪个方法了
//    @Override
//    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String method = req.getMethod();
//        if(method.equals("GET")){
//            doGet(req, resp);
//        }else if(method.equals("POST")){
//            doPost(req, resp);
//        }else if(method.equals("Delete")){
//            doDelete(req, resp);
//        }else if(method.equals("PUT")){
//            doPut(req, resp);
//        }
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("username"));
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            System.out.print(parameterNames.nextElement() + " ");
        }

        resp.setContentType("text/html; charset=utf-8");
        resp.getWriter().write("Get");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Post");
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Delete");
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Put");
    }

}
